(function() {
    var backtotop = document.getElementById('backtotop');
    //设置全局的定时器
    var timer;
    //返回顶部按钮的监听
    backtotop.onclick = function() {
        //设表先关
        clearInterval(timer);
        //设置定时器
        timer = setInterval(function() {
            document.documentElement.scrollTop -= 50;

            if (document.documentElement.scrollTop <= 0) {
                clearInterval(timer);
            }

        }, 20)
    };
    //监听页面的滚动
    window.onscroll = function() {
        //卷动值
        var scrollTop = document.documentElement.scrollTop || window.scrollY;
        if (scrollTop == 0) {
            backtotop.style.display = 'none';
        } else {
            backtotop.style.display = 'block';
        }
    }

})();